package com.qf.automation.user.Test;

import org.apache.ibatis.javassist.bytecode.ByteArray;

import java.io.*;
import java.util.Arrays;
import java.util.Scanner;

public class Test1 {
    public static void main1(String[] args) {
        int[] arr={1,2,3,4,5,6,7,8};
        Scanner sc=new Scanner(System.in);
        int i=sc.nextInt();
        int min=0;
        int max=arr.length-1;
        int maa=0;
        while (min<=max){
            maa=(min+max)/2;
            if (i<arr[maa]){
                max=maa-1;
            }else if (i>arr[maa]){
                min=maa+1;
            }else if (i==arr[maa]){
                System.out.println("下标为"+(maa-1));
                return ;
            }
        }
        System.out.println("没找到");

    }
    public static void main2(String[] args){
        int[] arr={92,1,50,43,20,98,40,9};
        int a=0;
        int b=arr.length-1;
        int c=0;
        for (;0<arr.length;){
            if (a<b) {
                if (arr[a] > arr[b]) {
                    arr[a] = arr[a] + arr[b];
                    arr[b] = arr[a] - arr[b];
                    arr[a] = arr[a] - arr[b];
                    b--;
                }
                if (arr[a] < arr[b]) {
                    arr[b] = arr[b] + arr[a];
                    arr[a] = arr[b] - arr[a];
                    arr[b] = arr[b] - arr[a];
                    a++;
                }
            }
            if (a==b){
                c=a;
                a=0;
                b=c-1;
            }
        }
        for (int i=0;i<arr.length;i++){
            System.out.print(arr[i] +" ");
        }
    }
    //二维数组转为稀疏数组再保存到本地文件,并取出来
    public static void main(String[] args) {
        int[][] arr=new int[3][3];
        arr[0][0]=11;
        arr[0][1]=11;
        arr[0][2]=2;
        arr[1][1]=2;
        arr[2][2]=4;
        File file=new File("E:\\新建文件夹\\二维文件.txt");
        try {
            FileOutputStream fileInputStream=new FileOutputStream(file);
            fileInputStream.write(toHotBytes(arr[0][0]));
            fileInputStream.close();
            FileInputStream fileInputStream1=new FileInputStream(file);
            byte[] bytes = new byte[1024];
            fileInputStream1.read(bytes);
            int i = toHotInteger(bytes);
            System.out.println(i);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    /**
     * 将byte数组转化为int
     */
    public static int toHotInteger(byte[] bytes){
        int value=0;
        for (int i=0;i<4;i++){
            int s=(4-1-i)*8;
            value+=(bytes[i]&0xFF)<<s;
        }
        return value;
    }
    /**
     * 将int转化为byte数组
     */
    public static byte[] toHotBytes(int i){
        byte[] b=new byte[4];
        b[0]=(byte) ((i>>24)&0xFF);
        b[1]=(byte) ((i>>16)&0xFF);
        b[2]=(byte) ((i>>8)&0xFF);
        b[3]=(byte) (i&0xFF);
        return b;
    }
}
